package com.muyu.danmo.service.impl;


import com.muyu.common.core.web.domain.BaseEntity;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.controller.DanmoCommunityCommentLikeController;
import com.muyu.danmo.domain.*;
import com.muyu.danmo.mapper.DanmoCommunityCommentLikeMapper;
import com.muyu.danmo.mapper.DanmoCommunityCommentMapper;
import com.muyu.danmo.service.IDanmoCommunityCommentService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 社区评论Service业务处理层
 *
 * @author : kunzong
 * @date : 2024-11-19
 */
@Service
public class DanmoCommunityCommentServiceImpl implements IDanmoCommunityCommentService {

    @Autowired
    private DanmoCommunityCommentMapper danmoCommunityCommentMapper;

    @Autowired
    private DanmoCommunityCommentLikeMapper danmoCommunityCommentLikeMapper;

    @Override
    public List<DanmoCommunityComment> selectDanmoCommunityCommentList(DanmoCommunityComment danmoCommunityComment) {
        // 返回根据查询条件得到的社区评论列表

        return danmoCommunityCommentMapper.selectDanmoCommunityCommentList(danmoCommunityComment);
    }

    @Override
    public List<DanmoCommunityComment> queryById(String id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // 根据 ID 查询单个帖子评论
        List<DanmoCommunityComment> danmoCommunityComment = danmoCommunityCommentMapper.selectDanmoCommunityCommentByPostId(id);
        //        获取帖子的点赞数，评论数和收藏数
        for (DanmoCommunityComment community : danmoCommunityComment) {
            communityAdd(community);
            if (loginUser != null) {
                if (community.getAuthorId().equals(loginUser.getUserid())) {
                    community.setIsMine("true");
                }
            }
        }
        timeDifference(danmoCommunityComment);
        return danmoCommunityComment;
    }

    @Override
    public int insert(DanmoCommunityComment danmoCommunityComment) {
        // 插入新的社区评论，并返回影响的行数
        //        查询父评论是否存在
        if (danmoCommunityComment.getParentCommentId() != null) {
            DanmoCommunityComment danmoCommunityComment1 = danmoCommunityCommentMapper.selectDanmoCommunityCommentByParentCommentId(String.valueOf(danmoCommunityComment.getParentCommentId()));
            if (danmoCommunityComment1 == null) {
                return 0;
            }
        }
        if (danmoCommunityComment.getContent() == null || danmoCommunityComment.getContent().trim().equals("")) {
            return 0;
        }
        return danmoCommunityCommentMapper.insertDanmoCommunityComment(danmoCommunityComment);
    }

    @Override
    public int update(DanmoCommunityComment danmoCommunityComment) {
        // 更新指定的社区评论，并返回影响的行数  
        return danmoCommunityCommentMapper.updateDanmoCommunityComment(danmoCommunityComment);
    }

    @Override
    public int deleteByIds(String id) {
        // 批量删除社区评论，并返回影响的行数
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();
        DanmoCommunityComment danmoCommunityComment = danmoCommunityCommentMapper.selectDanmoCommunityCommentById(id);
        if (danmoCommunityComment != null) {
            if (!userid.equals(danmoCommunityComment.getAuthorId())) {
                return 0;
            }
        } else {
            danmoCommunityCommentMapper.deleteDanmoCommunityCommentById(id);
            return 3;
        }
        danmoCommunityCommentMapper.deleteDanmoCommunityCommentById(id);
        danmoCommunityCommentMapper.deleteDanmoCommunityCommentByIds(id);
        return 1;
    }

    /**
     * 帖子点赞数，评论数，收藏数，是否关注
     *
     * @param community
     */
    private void communityAdd(DanmoCommunityComment community) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

//        判断是否点赞和收藏
        //        用户id
        Long userId = 0L;
        if (loginUser != null) {
            userId = loginUser.getUserid();
        }
        List<DanmoCommunityCommentLike> danmoCommunityCommentLikeList = danmoCommunityCommentLikeMapper.selectDanmoCollectMaterialByPostId(community.getId());
        community.setLikes((long) danmoCommunityCommentLikeList.size());
        if (danmoCommunityCommentLikeList != null) {
            if (userId != 0L) {
                for (DanmoCommunityCommentLike danmoCommunityCommentLike : danmoCommunityCommentLikeList) {
                    if (danmoCommunityCommentLike.getUserId().equals(userId)) {
                        community.setIsLike("true");
                    }
                }
            }
        }
    }


    /**
     * 时间差
     *
     * @param entities
     */
    public static <T extends BaseEntity> void timeDifference(List<T> entities) {
        LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
        for (T entity : entities) {
            Date updateTime = entity.getUpdateTime();
            LocalDateTime updateDateTime = updateTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            Duration duration = Duration.between(updateDateTime, now);

            long minutes = duration.toMinutes();
            long hours = duration.toHours();
            long days = duration.toDays();
            if (minutes < 1) {
                entity.setTimeAgo("刚刚");
            } else if (minutes < 60) {
                entity.setTimeAgo(minutes + " 分钟前");
            } else if (hours < 24) {
                entity.setTimeAgo(hours + " 小时前");
                System.out.println("Time difference: " + hours + " hours");
            } else if (days < 30) {
                entity.setTimeAgo(days + "天前");
                System.out.println("Time difference: " + days + " days");
            } else if (days < 365) {
                long months = days / 30;
                entity.setTimeAgo(months + "个月前");
                System.out.println("Time difference: " + months + " months");
            } else {
                long years = days / 365;
                entity.setTimeAgo(years + "年前");
                System.out.println("Time difference: " + years + " years");
            }
        }
    }
}